<%--
  Created by IntelliJ IDEA.
  User: 孤焰
  Date: 2021/5/24
  Time: 16:17
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" pageEncoding="utf-8" %>
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>收货地址列表</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="${pageContext.request.contextPath}/layuiadmin/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="${pageContext.request.contextPath}/layuiadmin/style/admin.css" media="all">
</head>
<body>

<div class="layui-fluid">
    <div class="layui-row layui-col-space15">
        <div class="layui-col-md12">
            <div class="layui-card">
                <div class="layui-card-header">
                    <a>收货地址列表</a>
                </div>
                <div class="layui-card-body">
                    <table class="layui-hide" id="shipping-list" lay-filter="shipping-list"></table>
                </div>
            </div>
        </div>
    </div>
</div>

<script src="${pageContext.request.contextPath}/layuiadmin/layui/layui.js"></script>
<script src="${pageContext.request.contextPath}/static/js/axios.min.js"></script>
<script>
    layui.config({
        base: '${pageContext.request.contextPath}/layuiadmin/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['index', 'table', 'form', 'layer', 'dropdown'], function(){
        var admin = layui.admin
        var table = layui.table;
        var form = layui.form;
        var dropdown = layui.dropdown;

        var tableWhere = {};
        tableWhere.userId = '${user.id}';
        table.render({
            id: 'shippingList',
            elem: '#shipping-list',
            url: '${pageContext.request.contextPath}/shipping/shippingList',
            where: tableWhere,
            cols: [[
                {type: 'checkbox'},
                {title: '收货人姓名', field: 'receiverName', width: 120},
                {title: '收货人电话', field:'receiverPhone', width: 150},
                {title: '省份', field:'receiverProvince', width: 150},
                {title: '城市', field:'receiverCity', width: 150},
                {title: '区/县', field:'receiverDistrict', width: 150},
                {title: '详细地址', field:'receiverAddress'},
                {title: '邮编', field:'receiverZip', width: 110}

            ]],
            page: true,
            toolbar: 'default'
        });

        <%--axios.get('${pageContext.request.contextPath}/category/categoryLeafList').then(function (res) {--%>
        <%--    var categoryList = [];--%>
        <%--    categoryList.push({--%>
        <%--        title: '全部类别',--%>
        <%--        id: null--%>
        <%--    });--%>
        <%--    for (var i = 0; i < res.data.data.length; i++) {--%>
        <%--        var category = res.data.data[i];--%>
        <%--        categoryList.push({--%>
        <%--            title: category.name,--%>
        <%--            id: category.id--%>
        <%--        });--%>
        <%--    }--%>

        <%--    //下拉菜单渲染--%>
        <%--    dropdown.render({--%>
        <%--        elem: '#shipping-category',--%>
        <%--        data: categoryList,--%>
        <%--        trigger: 'hover',--%>
        <%--        //菜单被点击事件--%>
        <%--        click: function(obj){--%>
        <%--            tableWhere.categoryId = obj.id;--%>
        <%--            table.reload('shippingList');--%>
        <%--        }--%>
        <%--    });--%>
        <%--});--%>

        //监听搜索表单
        // form.on('submit(search)', function (data) {
        //     tableWhere.search = data.field.search;
        //     table.reload('shippingList');
        //     return false;
        // });

        //监听单元格编辑
        <%--table.on('edit(shipping-list)', function (obj) {--%>
        <%--    var id = obj.data.id;--%>
        <%--    var price = obj.data.price;--%>
        <%--    var stock = obj.data.stock;--%>

        <%--    //价格正则校验--%>
        <%--    var priceReg = /(^[1-9][0-9]{0,7}$)|(^((0\.0[1-9]$)|(^0\.[1-9]\d?)$)|(^[1-9][0-9]{0,7}\.\d{1,2})$)/;--%>
        <%--    //正整数正则校验--%>
        <%--    var stockReg = /^[1-9]\d*$/;--%>
        <%--    if (priceReg.test(price) && stockReg.test(stock)) {--%>
        <%--        var param = {--%>
        <%--            id: id,--%>
        <%--            price: price,--%>
        <%--            stock: stock--%>
        <%--        }--%>

        <%--        axios.post('${pageContext.request.contextPath}/shipping/updateShippingById', param).then(function (res){--%>
        <%--            if (res.data.code == 200) {--%>
        <%--                layer.msg('收货地址修改成功', {icon: 6});--%>
        <%--            } else {--%>
        <%--                layer.msg('收货地址修改失败', {icon: 5});--%>
        <%--            }--%>
        <%--            --%>
        <%--        });--%>
        <%--    } else {--%>
        <%--        layer.msg("值不合法", {icon: 5});--%>
        <%--        // TODO 提示完不合法之后单元格没有恢复正确值--%>
        <%--    }--%>
        <%--});--%>

        //单元格表头工具栏监听
        table.on('toolbar(shipping-list)', function(obj) {
            switch (obj.event) {
                case 'add':
                    top.layui.index.openTabsPage('${pageContext.request.contextPath}/shipping/shippingAdd', "收货地址增加");
                    break;
                case 'delete':
                    var checkBoxData = table.checkStatus('shippingList').data;
                    if (checkBoxData.length == 0) {
                        layer.msg('未选中数据项', {icon: 5});
                        return;
                    }

                    layer.confirm('你确认删除吗?', {title:'提示'}, function(index){
                        var idArr = [];
                        for (var i = 0; i < checkBoxData.length; i++) {
                            idArr.push(checkBoxData[i].id);
                        }

                        axios.post('${pageContext.request.contextPath}/shipping/batchDelShipping', idArr).then(function (res) {
                            if (res.data.code != 200) {
                                layer.msg('数据库遇到未知错误，删除失败', {icon: 5});
                            } else {
                                layer.msg('数据删除成功', {icon: 6});
                                table.reload('shippingList');
                            }
                        });

                        layer.close(index);
                    });
                    break;
                case 'update':
                    var checkBoxData = table.checkStatus('shippingList').data;
                    if (checkBoxData.length == 0) {
                        layer.msg('未选中数据项', {icon: 5});
                        return;
                    }
                    if (checkBoxData.length > 1) {
                        layer.msg('数据项不可以多选', {icon: 5});
                        return;
                    }

                    layer.open({
                        type: 2,
                        title: '收货地址编辑',
                        shadeClose: false,
                        shade: 0,
                        area: ['60%', '90%'],
                        content: '${pageContext.request.contextPath}/shipping/shippingUpdate?id='+checkBoxData[0].id //iframe的url
                    });
                    break;
            }
        });
    });
</script>
</body>
</html>